package com.panshi.controller.usercenter;

import com.panshi.api.IMessagApi;
import com.panshi.api.UserGetApi;
import com.panshi.utils.GetSessionUser;
import com.panshi.utils.RedisUtils;
import com.panshi.vo.ResponseVO;
import com.panshi.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private UserGetApi userGetApi; //APi类

    @Autowired
    RedisUtils redisUtils; //redis工具类

    @Autowired
    private IMessagApi messagApi; //站内消息api

    @RequestMapping("/login")//登录页面
    public String login() {
        return "login";
    }

    /**
     * 重新打开找回密码页面
     *
     * @return
     */
    @RequestMapping("/findpwd")
    public String jumpfindpwd() {
        return "findpwd";
    }

    /**
     * 提交密码
     *
     * @param userName 找回的邮箱
     * @param code     短信/邮箱验证码
     * @param pwd      新密码
     * @return
     */
    @RequestMapping("/submitfindpwd")
    @ResponseBody
    public ResponseVO submitFindPwd(String userName, String code, String pwd) {
        String type = "findpwd";
        ResponseVO rs = new ResponseVO();
        //从缓存里拿到刚才发送的邮箱验证码
        Jedis jedis = redisUtils.getJedis();
        String sendCode = jedis.get(type + ":" + userName);
        //邮箱验证码正确
        if (code.equals(sendCode)) {
            //根据邮箱拿到该用户的id
            UserVO userVO = userGetApi.findUserByEmail(userName);
            if (userVO != null) {
                //调用修改密码方法
                userGetApi.updateUserPwd(userVO.getU_id() + "", pwd);
                rs.setState(200);
                rs.setSuccess(true);
                rs.setMsg("密码找回成功！请重新登录");
            } else {
                rs.setSuccess(false);
                rs.setState(500);
                rs.setMsg("该账号不存在！");
            }
        } else {
            rs.setMsg("邮箱验证码错误！");
            rs.setSuccess(false);
            rs.setState(500);
        }
        return rs;
    }

    /**
     * 找回密码发送验证码
     *
     * @param userName 找回的邮箱
     * @param code     图形验证码
     * @return
     */
    @RequestMapping("/sendcode/findpwd")
    @ResponseBody
    public ResponseVO sendCodeFindpwd(String userName, String code, HttpSession session) {
        ResponseVO rs = new ResponseVO();
        //session中储存的图形验证码
        String kaptcha = session.getAttribute("kaptcha") + "";
        //验证码验证成功
        if (code.equals(kaptcha)) {
            UserVO userVO = userGetApi.findUserByEmail(userName);
            if (userVO != null) {
                //用户存在 可以发送验证码
                //发送验证码
                rs = userGetApi.sendEmail(userName, "findpwd");
            } else {
                rs.setSuccess(false);
                rs.setState(500);
                rs.setMsg("该账号不存在！");
            }
        } else {
            rs.setMsg("图形验证码错误！");
            rs.setSuccess(false);
            rs.setState(500);
        }
        return rs;
    }

    @RequestMapping("/login/out")//退出登录
    @ResponseBody
    public ResponseVO loginOut(HttpSession session, Model model) {
        //删除session里面的User
        session.removeAttribute("loginUser");

        //删除model里的登录用户
        model.addAttribute("loginUser",null);
        ResponseVO rs = new ResponseVO();
        System.out.println("返回到登录页");
        rs.setSuccess(true);
        rs.setMsg("login/out退出成功！");
        return rs;
    }

    @RequestMapping("/index")
    public String initIndex(Model model) {
        //从session里面拿到用户
        UserVO loginUser = GetSessionUser.getSessionUser();
        //登录过了
        if (loginUser != null) {
            System.out.println("登录的mssagApi"+messagApi);
            System.out.println("登录的loginUser"+loginUser);
            System.out.println("登录的messagApi.inquireSum(loginUser.getU_id())"+messagApi.inquireSum(loginUser.getU_id()));
/*           //登录的用户id存入redis中
            Jedis jedis = redisUtils.getJedis();
            jedis.set("loginUserId", loginUser.getU_id() + "", "NX", "EX", 300);*/
            model.addAttribute("loginUser", loginUser);
            //获取该用户的站内消息数量
            int mesgCount =messagApi.inquireSum(loginUser.getU_id()).getData();
            model.addAttribute("mesgCount",mesgCount);
        }
        return "index";
    }

    @RequestMapping("/loginVali") //web端验证图片验证码
    @ResponseBody
    public ResponseVO loginVali(String userName, String pwd, String imgcode, HttpSession session,Model model) {
        //拿到session里面的图片验证码
        String kaptcha = session.getAttribute("kaptcha") + "";
        System.out.println("正在验证图形验证码-------------" + kaptcha);
        ResponseVO rs = new ResponseVO();
        //图片验证码正确
        if (imgcode != null && imgcode.equals(kaptcha)) {
            rs = userGetApi.loginValidate(userName, pwd);
            if (rs.isSuccess()) {
                System.out.println("登录验证密码成功了！=======");
                session.setAttribute("loginUser", rs.getUserVO());
                model.addAttribute("loginUser",rs.getUserVO());
                rs.setUserVO(null);
            } else {
                rs.setSuccess(false);
                rs.setState(499);
                rs.setMsg("密码输入错误！");
            }
        }else{
            rs.setSuccess(false);
            rs.setState(500);
            rs.setMsg("验证码输入错误！");
        }

        return rs;
    }
}
